package com.timoon.webcomponent.utility.product.single;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.app.resource.image.ImageLocation;
import com.timoon.component.panel.bootstrap.modal.ModalPanel;
import com.timoon.page.Page1;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.page.SingleProductPage;
import com.timoon.webcomponent.utility.common.HorizontalTab4Panel;
import com.timoon.webcomponent.utility.common.HorizontalTab4Parameter;
import com.timoon.webcomponent.utility.product.BaseProductPanel;
import com.timoon.webcomponent.utility.product.discount.Discount1Panel;
import com.timoon.webcomponent.utility.product.discount.Discount1Service;
import com.timoon.webcomponent.utility.product.single.component.ProductCommentListPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductDetailTabPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductPackListPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductServiceAfterSalePanel;
import com.timoon.webcomponent.utility.product.sort.sortnav1.InsideNavsService;
import com.timoon.webcomponent.utility.product.sort.sortnav2.Sortnav2Panel;

/**
 * <p>
 * 显示一个商品详细界面
 * </p>
 * <p>
 * PageParameters:product_id
 * </p>
 *  *
 * <p>
 * public
 * </p>
 *  *
 * <p>
 * Company:timoon
 * </p>
 *  *@author 付乐天  *@version 1.0 
 */

public class BaseSingleProductPanel extends BaseProductPanel {

	@SpringBean(name = "com.timoon.webcomponent.utility.product.service.ProductService")
	private SingleProductService service;
	
	private PageParameters pageParameters;

	private Product product;

	public BaseSingleProductPanel(String id, PageParameters pageParameters) {
		super(id);
		// TODO Auto-generated constructor stub
		if (pageParameters.get("product_id").isNull()) {
			throw new RuntimeException("缺失product_id参数");
		}

		this.pageParameters = pageParameters;

		this.product = this.service.loadProductById(this.pageParameters.get(
				"product_id").toInt());

		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		//大图
		renderPic();

		//明细
		renderBaseProduct();

		//tab
		renderTabs();

		// 商品分类
		renderSort();

		// 【加入购物车】
		renderAddCartBtn();

		// 【店长推荐】
		renderBoos();
	}

	private void renderBoos() {
		// TODO Auto-generated method stub
//		add(new AjaxLazyLoadPanel("wk_boss_recomment") {
//
//			@Override
//			public Component getLazyLoadComponent(String arg0) {
//				// TODO Auto-generated method stub
//				return (new Discount1Panel(arg0,"店长推荐",SingleProductPage.class));
//			}
//		});
	}

	protected void renderSort() {
		// TODO Auto-generated method stub
		add(new Sortnav2Panel("wk_sort_nav", Page1.class));
	}

	private void renderAddCartBtn() {
		// TODO Auto-generated method stub
		Form form = new Form("wk_form");
		add(form);

		final ModalPanel modal = new ModalPanel("wk_cart_modal", "添加成功",
				"去填写订单","已成功添加到购物车！") {

			@Override
			public boolean onAction(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				super.onAction(target);
				System.err.println("fwaefawe");
				this.setResponsePage(Page1.class);
				return true;
			}
		};
		add(modal);

		form.add(new AjaxButton("wk_add_cart") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				modal.showModal(target);
			}

		});
	}

	private void renderTabs() {
		// TODO Auto-generated method stub

		add(new AjaxLazyLoadPanel("wk_tabs") {

			@Override
			public Component getLazyLoadComponent(String arg0) {
				// TODO Auto-generated method stub
				HorizontalTab4Parameter parameter = new HorizontalTab4Parameter();
				parameter.setLabels(new String[] { "商品介绍", "包装清单", "商品评价",
						"售后保障" });
				parameter
						.setPanels(new Panel[] {
								new ProductDetailTabPanel("wk_inner1", product
										.getId(), 4),
								new ProductPackListPanel("wk_inner2", product),
								new ProductCommentListPanel("wk_inner3",
										product.getId()),
								new ProductServiceAfterSalePanel("wk_inner4",
										product) });

				return new HorizontalTab4Panel(
						AjaxLazyLoadPanel.LAZY_LOAD_COMPONENT_ID, parameter);
			}
		});
	}

	private void renderBaseProduct() {
		// TODO Auto-generated method stub
		add(new Label("wk_name", product.getName()));

		add(new Label("wk_market_price", product.getMarketPrice()));

		add(new Label("wk_actrue_price", product.getActualPrice()));

		add(new Label("wk_sale_info", product.getSaleInfo()));
	}

	private void renderPic() {
		// TODO Auto-generated method stub
		add(new Image("wk_img", new PackageResourceReference(ImageLocation.class,
				this.product.getPhoto())));
	}

	public SingleProductService getService() {
		return service;
	}

	public void setService(SingleProductService service) {
		this.service = service;
	}

}
